package com.pearadmin.pro.modules.ser.rest;

import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.ser.pojo.FileDTO;
import com.pearadmin.pro.modules.ser.pojo.WebSSHData;
import com.pearadmin.pro.modules.ser.service.ServerInfoService;
import com.pearadmin.pro.modules.ser.service.SftpService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Author: didiplus
 * Email: 972479352@qq.com
 * CreateTime: 2022/3/15
 * Desc: SFTP接口层
 */
@Api(tags = {"sftp"})
@RestController
@RequestMapping(ControllerConstant.PREFIX + "sftp")
public class SftpController extends BaseController {

    @Resource
    SftpService sftpService;

    @Resource
    ServerInfoService serverInfoService;

    @PostMapping("connect")
    public Result connect( @RequestBody String hostId) throws Exception {
        WebSSHData webSSHData = serverInfoService.selectserverById(hostId);
        return success(sftpService.connect(webSSHData));
    }

    @PostMapping("ls")
    public Result<List<FileDTO>> listFiles(@RequestBody Map<String,String> mapdata) throws Exception{
        System.out.println("mapdata = " + mapdata);
        String keys = mapdata.get("keys");
        String path = mapdata.get("path");
        return Result.success(sftpService.listFiles(keys,path));
    }
}
